/**
 Do not return anything, modify board in-place instead.
 */
function solve(board: string[][]): void {
    const dx = [-1, 0, 0, 1];
    const dy = [0, -1, 1, 0];
    const m = board.length;
    const n = board[0].length;
    const visit: boolean[][] = Array.from({ length: m }, () => new Array(n).fill(false));
    const ans: number[][] = [];

    const dfs = (_board: string[][], x: number, y: number) => {
        visit[x][y] = true;
        for (let i = 0; i < 4; i++) {
            const nx = x + dx[i];
            const ny = y + dy[i];

            if (nx < 0 || ny < 0 || nx >= m || ny >= n) {
                continue;
            }

            if (_board[nx][ny] === 'O' && !visit[nx][ny]) {
                ans.push([nx, ny]);
                dfs(_board, nx, ny);
            }
        }
    };

    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (board[i][j] === 'O' && !visit[i][j]) {
                ans.push([i, j]);
                dfs(board, i, j);
            }
        }
    }

    const sides = ans.filter(([x, y]) => x === 0 || y === 0 || x === m - 1 || y === n - 1);
    const parsed: Set<number[]> = new Set();
    const findNodes = (points: number[][]) => {
        if (points.filter((point) => parsed.has(point)).length === points.length) {
            return;
        }

        for (const point of points) {
            parsed.add(point);
            const [x, y] = point;
            for (let i = 0; i < 4; i++) {
                const nx = x + dx[i];
                const ny = y + dy[i];

                const arr = ans.filter((item) => item[0] === nx && item[1] === ny);
                if (arr.length) {
                    findNodes(arr);
                }
            }
        }
    };

    findNodes(sides);
    ans.filter((item) => !parsed.has(item)).forEach(([x, y]) => {
        board[x][y] = 'X';
    })
};